require 'test/unit'
require 'treenode.rb'
require 'treeservice.rb'

class Test_TreeNode_And_Service < Test::Unit::TestCase
    
    def test_Search_Arbitrary
        myTree = TreeNode.new('root')

        myTree.addChild(TreeNode.new('parent'))
        myTree.childNodes[0].addChild(TreeNode.new('child1'))
        myTree.childNodes[0].childNodes[0].addChild(TreeNode.new('child1_sibling1'))
        myTree.childNodes[0].addChild(TreeNode.new('child2'))
        
        myMagicSearchString = 'child2_sibling1'
        myTree.childNodes[0].childNodes[0].addChild(TreeNode.new(myMagicSearchString))
        
        puts "searchValue=" + myMagicSearchString
        returnedNodes = TreeService.instance.search(myTree, myMagicSearchString)
        
        puts "returnedNodes="
        returnedNodes.each { |node| print "\t" + node.value + "\n" }
        
        assert_equal(1, returnedNodes.length)
        assert_equal(myMagicSearchString, returnedNodes[0].value)
    end
    
    def test_Print_Graph_From_Excercise_Sample_Search_Returns_Two_Results
        myTree = TreeNode.new('root')
        myTree.addChild(TreeNode.new('Film'))
        myTree.childNodes[0].addChild(TreeNode.new('Tapes'))
        myTree.childNodes[0].childNodes[0].addChild(TreeNode.new('Media'))
        
        myMagicSearchString = 'Records'
        
        myTree.childNodes[0].addChild(TreeNode.new(myMagicSearchString))
        
        myTree.childNodes[0].childNodes[1].addChild(TreeNode.new('Media'))
        
        myTree.addChild(TreeNode.new('Movies'))
        myTree.childNodes[1].addChild(TreeNode.new('Tapes'))
        myTree.childNodes[1].childNodes[0].addChild(TreeNode.new('Media'))
        
        myTree.childNodes[1].addChild(TreeNode.new(myMagicSearchString))
        myTree.childNodes[1].childNodes[1].addChild(TreeNode.new('Media'))
        
        puts "searchValue=" + myMagicSearchString
        returnedNodes = TreeService.instance.search(myTree, myMagicSearchString)
   
        assert_equal(2, returnedNodes.length)
        
        assert_equal(myMagicSearchString, returnedNodes[0].value)
        assert_equal(myMagicSearchString, returnedNodes[1].value)
        
        puts "returnedNodes (printGraph)="
        returnedNodes.each { |node| 
            returnedGraph = TreeService.instance.printGraph(node, '|')
            assert(true, (returnedGraph.count(myMagicSearchString) != 0))
            print "\t" + returnedGraph + "\n" 
        }
    end
    
    def test_CloneNode
        myNode = TreeNode.new('Original_Node')
        
        myClonedNode = TreeService.instance.cloneNode(myNode)
        
        assert_not_equal(myNode, myClonedNode)
        assert_equal(myNode.value, myClonedNode.value)
    end
    
    def test_CloneNode_Verify_ClonedChildNodes
        myNode = TreeNode.new('Original_Node')
        
        myNode.addChild(TreeNode.new("Original_Child_Node"))
        myNode.childNodes[0].addChild(TreeNode.new('Original_Sub_Child_Node'))
        
        myClonedNode = TreeService.instance.cloneNode(myNode)
        
        assert_not_equal(myNode.childNodes[0], myClonedNode.childNodes[0])
        assert_equal(myNode.childNodes[0].value, myClonedNode.childNodes[0].value)
        
        assert_not_equal(myNode.childNodes[0].childNodes[0], myClonedNode.childNodes[0].childNodes[0])
        assert_equal(myNode.childNodes[0].childNodes[0].value, myClonedNode.childNodes[0].childNodes[0].value)
    end
end